package as.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import as.entity.AttendanceRecord;
import as.entity.Course;
import as.entity.Recycler;

import as.entity.Student;

public class StudentDAO extends HibernateDaoSupport {

	@SuppressWarnings("unchecked")
	public void saveStudent(Student student) {

		this.getHibernateTemplate().saveOrUpdate(student);

		/*
		 * final String hql1 = "from Recycler order by registerNo asc";
		 * List<Recycler> list1 = new ArrayList(); list1 =
		 * this.getHibernateTemplate().find(hql1); if(list1.size()==0){ final
		 * String hql = "from Student order by registerNo desc"; List<Student>
		 * list = new ArrayList(); list = this.getHibernateTemplate().find(hql);
		 * if (list.size() == 0) { student.setRegisterNo(00001); } else { int i
		 * = list.get(0).getRegisterNo(); student.setRegisterNo(i + 1);
		 * 
		 * } } else { int i= list1.get(0).getRegisterNo();
		 * student.setRegisterNo(i);
		 * this.getHibernateTemplate().delete(list1.get(0));
		 * 
		 * } this.getHibernateTemplate().save(student);
		 */

	}

	public void removeStudent(Student student) {

		this.getHibernateTemplate().delete(student);
	}

	public void updateStudent(Student student) {

		this.getHibernateTemplate().update(student);
	}

	public Student findStudentIs(long id) {

		Student student = (Student) this.getHibernateTemplate().get(
				Student.class, id);
		return student;
	}

	public List<Student> getStudentOfCourse(long courseId) {
		String hql = "select register.student from Register as register where"
				+ " register.course = ? ";
		Session session = this.getSession(true);
		List<Student> list = (List<Student>) session.createQuery(hql).setLong(
				0, courseId).list();

		session.close();

		return list;

	}

}
